﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WiMo.Games.GameEngine.WP7S;
using Collisions;

namespace WP7S.Collisions
{
    public partial class App : Application
    {
        public App()
        {
            UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(Application_UnhandledException);
            this.Startup += this.Application_Startup;
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var engine = new GameEngine();
            var targetSize = new WiMo.Games.Size(240, 320);

            engine.Width = (double)480;
            engine.Height = (double)800;

            var actualSize = new WiMo.Games.Size((int)engine.Width, (int)engine.Height);
            engine.StateManager = new StateManager();
            engine.Display = new WiMo.Games.Drawing.WP7S.DisplayManager(engine, targetSize, actualSize, 96);
            engine.InputManager = new WiMo.Games.StandardInput.InputManager(engine, engine.Display);
            engine.PhysicsManager = new WiMo.Games.StandardPhysics.PhysicsManager();
            engine.CollisionManager = new WiMo.Games.StandardCollisions.CollisionManager(targetSize);
            engine.AudioManager = new WiMo.Games.StandardAudio.AudioManager(engine);
            engine.MenuManager = new WiMo.Games.StandardMenus.MenuManager(engine);

            var game = new Game(engine);

            engine.Initialize(game);

            this.RootVisual = engine;

            engine.Start();            

        }

        // Code to execute on Unhandled Exceptions
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred, break in the debugger
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                // By default show the error
                e.Handled = true;
                MessageBox.Show(e.ExceptionObject.Message + Environment.NewLine + e.ExceptionObject.StackTrace,
                    "Error", MessageBoxButton.OK);
            }
        }

        private void PhoneApplicationService_Launching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e)
        {

        }

        private void PhoneApplicationService_Activated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
        {

        }

        private void PhoneApplicationService_Closing(object sender, Microsoft.Phone.Shell.ClosingEventArgs e)
        {

        }

        private void PhoneApplicationService_Deactivated(object sender, Microsoft.Phone.Shell.DeactivatedEventArgs e)
        {

        }
 
    }
}